package com.aistar.serviceVO.impl;

import com.aistar.mapper.ChoiceMapper;
import com.aistar.mapper.QuestionDetailMapper;
import com.aistar.mapper.QuestionSetMapper;
import com.aistar.pojo.*;
import com.aistar.pojoVO.QuestionDetailVO;
import com.aistar.serviceVO.QuestionDetailVOService;
import com.aistar.util.MessageUtil;
import com.aistar.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class QuestionDetailVOServiceImpl implements QuestionDetailVOService {
    private final QuestionSetMapper questionSetMapper;
    private final QuestionDetailMapper questionDetailMapper;
    private final ChoiceMapper choiceMapper;

    @Autowired
    public QuestionDetailVOServiceImpl(QuestionSetMapper questionSetMapper,
                                       QuestionDetailMapper questionDetailMapper,
                                       ChoiceMapper choiceMapper) {
        this.questionSetMapper = questionSetMapper;
        this.questionDetailMapper = questionDetailMapper;
        this.choiceMapper = choiceMapper;
    }

    @Override
    public ServerResponse getByCustomerId(Integer customerId) {
        QuestionSetExample example = new QuestionSetExample();
        example.createCriteria()
                .andStatusNotEqualTo(MessageUtil.STATUS_FAIL)
                .andCustomerIdEqualTo(customerId);
        example.setOrderByClause("question_set_create_time desc");
        List<QuestionSet> questionSetList = questionSetMapper.selectByExample(example);
        if (questionSetList != null && questionSetList.size() > 0) {
            List<QuestionDetailVO> questionDetailVOList = new ArrayList<>();
            for (QuestionSet questionSet : questionSetList) {
                QuestionDetailExample questionDetailExample = new QuestionDetailExample();
                questionDetailExample.createCriteria()
                        .andQuestionSetIdEqualTo(questionSet.getQuestionSetId());
                List<QuestionDetail> questionDetailList = questionDetailMapper.selectByExample(questionDetailExample);
                for (QuestionDetail questionDetail : questionDetailList) {
                    Choice choice = choiceMapper.selectByPrimaryKey(questionDetail.getChoiceId());
                    questionDetailVOList.add(new QuestionDetailVO(questionDetail, questionSet, choice));
                }
            }
            return ServerResponse.getSuccess(questionDetailVOList);
        }
        return ServerResponse.getFailed();
    }

    @Override
    public ServerResponse getByQuestionSetId(Integer questionSetId) {
        QuestionSet questionSet = questionSetMapper.selectByPrimaryKey(questionSetId);
        if (questionSet != null) {
            QuestionDetailExample example = new QuestionDetailExample();
            example.createCriteria().andQuestionSetIdEqualTo(questionSetId);
            List<QuestionDetail> questionDetailList = questionDetailMapper.selectByExample(example);

            List<QuestionDetailVO> questionDetailVOArrayList = new ArrayList<>();
            for (int i = 0; i < questionDetailList.size(); i++) {
                QuestionDetail questionDetail = questionDetailList.get(i);
                Integer choiceId = questionDetail.getChoiceId();
                Choice choice = choiceMapper.selectByPrimaryKey(choiceId);
                QuestionDetailVO questionDetailVO = new QuestionDetailVO(questionDetail, questionSet, choice);
                questionDetailVOArrayList.add(questionDetailVO);
            }
            return ServerResponse.getSuccess(questionDetailVOArrayList);
        }
        return ServerResponse.getFailed();
    }

}
